<%@page import="org.zty.service.AttrService"%>
<%@page import="org.zty.domain.Attr"%>
<%@page import="java.util.ArrayList"%>
<%@page import="org.zty.util.RequestUtil"%>
<%@page import="java.util.Date"%>
<%@page import="java.util.List"%>
<%@page import="org.zty.service.PojoService"%>
<%@page import="org.zty.domain.Pojo"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@include file="common/taglib.jsp" %>
<%
String m = RequestUtil.toStr(request,"m","");
if("add".equals(m)){
	
	String name = RequestUtil.toStr(request,"pojo_name","");
	String descstr = RequestUtil.toStr(request,"descstr","");
	
	Pojo pojo = new Pojo();
	
	pojo.setName(name);
	pojo.setDescstr(descstr);
	pojo.setCrTime(new Date());
	pojo.setUpdateTime(new Date());
	PojoService ps= new PojoService();
	pojo = ps.insertPojo(pojo);
	
	String[] attrtypes = request.getParameterValues("attr_type");
	String[] attrnames = request.getParameterValues("attr_name");
	List<Integer> typeList = new ArrayList();
	List<String> nameList = new ArrayList();
	for (String str : attrtypes) {
		if ("".equals(str.trim())) {
			continue;
		}
		if ("string".equals(str.trim().toLowerCase())) {
			typeList.add(1);
		} else if ("int".equals(str.trim().toLowerCase())) {
			typeList.add(2);
		} else if ("date".equals(str.trim().toLowerCase())) {
			typeList.add(3);
		} else {
			typeList.add(4);
		}
	}
	for (String str : attrnames) {
		if ("".equals(str.trim())) {
			continue;
		}
		nameList.add(str);
	}
	
	if (nameList.size() != typeList.size() || nameList.size() == 0) {
		return;
	}
	List<Attr> attrList = new ArrayList<Attr>();
	AttrService as = new AttrService();
	for (int i = 0; i < nameList.size(); i++) {
		Attr attr = new Attr();
		attr.setType(typeList.get(i));
		attr.setName(nameList.get(i));
		attr.setParentId(pojo.getId());
		as.insertAttr(attr);
	}
	out.println("{\"msg\":\"true\"}");

}else if("update".equals(m)){
	int id = RequestUtil.toInt(request, "id",0);
	String name = RequestUtil.toStr(request,"pojo_name","");
	String descstr = RequestUtil.toStr(request,"descstr","");
	Pojo pojo = new Pojo();
	PojoService ps= new PojoService();
	pojo.setId(id);
	pojo =ps.getPojo(pojo);
	pojo.setName(name);
	pojo.setDescstr(descstr);
	pojo.setUpdateTime(new Date());
	
	ps.updatePojo(pojo);
	
	Attr attrtmp= new Attr();
	attrtmp.setParentId(id);
	AttrService assss = new AttrService();
	assss.deleteAttr(attrtmp);
	
	
	String[] attrtypes = request.getParameterValues("attr_type");
	String[] attrnames = request.getParameterValues("attr_name");
	List<Integer> typeList = new ArrayList();
	List<String> nameList = new ArrayList();
	for (String str : attrtypes) {
		if ("".equals(str.trim())) {
			continue;
		}
		if ("string".equals(str.trim().toLowerCase())) {
			typeList.add(1);
		} else if ("int".equals(str.trim().toLowerCase())) {
			typeList.add(2);
		} else if ("date".equals(str.trim().toLowerCase())) {
			typeList.add(3);
		} else {
			typeList.add(4);
		}
	}
	for (String str : attrnames) {
		if ("".equals(str.trim())) {
			continue;
		}
		nameList.add(str);
	}
	
	if (nameList.size() != typeList.size() || nameList.size() == 0) {
		return;
	}
	List<Attr> attrList = new ArrayList<Attr>();
	AttrService as = new AttrService();
	for (int i = 0; i < nameList.size(); i++) {
		Attr attr = new Attr();
		attr.setType(typeList.get(i));
		attr.setName(nameList.get(i));
		attr.setParentId(pojo.getId());
		as.insertAttr(attr);
	}
	out.println("{\"msg\":\"true\"}");

}else if("delete".equals(m)){
	int id = RequestUtil.toInt(request,"id",0);
	PojoService ps= new PojoService();
	Pojo pojo = new Pojo();
	pojo.setId(id);
	ps.deletePojo(pojo);
	Attr attr= new Attr();
	attr.setParentId(id);
	AttrService as = new AttrService();
	as.deleteAttr(attr);
	out.println("{\"msg\":\"true\"}");
}
%>
